﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Taxi_taxi.Resources;
using Windows.Devices.Geolocation;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Phone.Maps.Controls;
using System.Device.Location;
using Microsoft.Phone.Tasks;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Models;
using TaxiServices;

namespace Taxi_taxi.View
{
    public partial class MapPage : PhoneApplicationPage
    {

        private ObservableCollection<TaiXe> _taixe;
        public ObservableCollection<TaiXe> ListTaiXe
        {
            get { return _taixe; }
            set
            {
                _taixe = value;              
            }
        }

        private double lat;
        private double lon;

        private TaxiClient client;

        

        public MapPage()
        {
            InitializeComponent();
            Loaded += MapPage_Loaded;
            
            
        }

        void MapPage_Loaded(object sender, RoutedEventArgs e)
        {
            GoMap();
            GetListTaiXe(11.052626, 106.66775);
            MessageBox.Show("Xong");
        }


        private void GetListTaiXe(double lat, double lon)
        {
            client = new TaxiClient();
            client.GetTaiXeByLocationAsync(lat, lon);
            client.GetTaiXeByLocationCompleted += client_GetTaiXeByLocationCompleted;
        }
        void client_GetTaiXeByLocationCompleted(object sender, GetTaiXeByLocationCompletedEventArgs e)
        {
            ListTaiXe = e.Result;

        }
        private StackPanel CreatePushpin()
        {
            StackPanel stack = new StackPanel();
            stack.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            stack.Background = new SolidColorBrush(Colors.Transparent);
            Ellipse ellipse = new Ellipse();
            ellipse.Fill = new SolidColorBrush(Colors.Black);
            ellipse.Height = 20; ellipse.Width = 20;
            TextBlock tb = new TextBlock();
            tb.Text = "I'm here!!!";
            tb.Foreground = new SolidColorBrush(Colors.Red);

            //Adding the Rectangle to the Grid 
            stack.Children.Add(ellipse);
            stack.Children.Add(tb);
            return stack;
        }
        private async void GoMap()
        {
            Geolocator geolocator = new Geolocator();
            geolocator.DesiredAccuracyInMeters = 50;
            try
            {
                Geoposition geoposition = await geolocator.GetGeopositionAsync(maximumAge: TimeSpan.FromMinutes(5), timeout: TimeSpan.FromSeconds(10));
                //MessageBox.Show("XDVT xong (" + geoposition.Coordinate.Latitude + ", " + geoposition.Coordinate.Longitude + ")");
                lat = geoposition.Coordinate.Latitude;
                lon = geoposition.Coordinate.Longitude;
                map.Center = new System.Device.Location.GeoCoordinate(lat,lon);
                map.ZoomLevel = 13;
                map.Layers.Clear();
                StackPanel MyGrid = CreatePushpin();
                MapOverlay MyOverlay = new MapOverlay();
                MyOverlay.Content = MyGrid;
                MyOverlay.GeoCoordinate = map.Center;
                MyOverlay.PositionOrigin = new Point(0, 0);
                MapLayer MyLayer = new MapLayer();
                MyLayer.Add(MyOverlay);
                map.Layers.Add(MyLayer);

                MessageBox.Show("Show!!!");
            }
            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("Error");
            }
            catch (TaskCanceledException)
            {
                MessageBox.Show("Cancel");
            }
        }

    }
}